<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mod_Admin2 extends CI_Controller {
    //phan trang
    private $base_url_trang = '#';
    private $total_rows = 100;
    private $per_page = 10;
    private $limit = 10;
    //
    private $baiviet = 'baiviet'; // Tên bảng

    
    function  __construct(){
        parent::__construct();
        //$this->session->sess_destroy();
    }
	public function index(){
	}
    public function xuly($function){
        $page = $this->security->xss_clean($this->uri->segment(4,0));// Ten duong dan den lay bang nao
        $action = $this->security->xss_clean($this->uri->segment(5,0)) ;// Ten duong dan lay den action nao
        $id = $this->security->xss_clean($this->uri->segment(6,0));// Ten duong dan lay den id nao
        if(!empty($page)){
            $table = $this->$page;
            $page = $this->$page;
        }else $table = ''; // chua kiem tra co bang hay chua
        $this->base_url_trang = base_url().'backend/admin/'.$function.'/'.$page;

        if(!is_numeric($id)){ $id = 0;}

        if(!empty($action)){
            switch ($action) {
               case 'list':
                    //hien thi danh sach
                    if($table != $this->session->userdata('sec_table')){
                        //check xem co trung bang hay khong de kiem tra xem dang o trang cu hoac trang moi xoa session//bang ba
                        // bang bang session bang thi kiem tra order_by
                        $this->session->set_userdata('order',0);
                        $this->session->set_userdata('sec_table',$table);
                    }
                    if($this->session->userdata('order')){
                        $order = array('truong'=>$this->session->userdata('order'),
                                        'sapxep'=>$this->session->userdata('sapxep'));
                    }else $order=array('truong'=>'id','sapxep'=>'DESC');
                    
                     // limit start dung sau 
                    if(!$this->session->userdata('limit')){
                        $this->session->set_userdata('limit',$this->limit);
                    }
                    else $this->limit = $this->session->userdata('limit');
                    
                    $this->per_page = $this->limit;// so record tren 1 trang bang limit                
                    
                    if($id){
                         $srart_limit = $id;
                    }else $srart_limit = 0;
                    $limit = $this->limit;
                    
                    //where
                    $where = array();
                    $data1 = $this->home_model->get_data($table,$where,$order,$limit,'',$srart_limit);
                    $data= array('data'=>$data1);
                    $this->total_rows =$this->home_model->get_number($table,$where='',$limit='');
                    $this->phantrang();
                    $this->load->view('mod_admin2/'.$page.'_list',$data);//true la k hien chi tra ve
                    break;
            
               case 'edit' :
                    $this->load->library('form_validation');
                    $this->khaibao($table); // khai bao form_validation
                    
                    if(!empty($_POST)){
                        if ($this->form_validation->run() == FALSE){}
                        else{
                            $data = $this->xuly_dulieu($_POST,$table,$param = $id);
                            if($id){
                                $this->db->where('id', $id);
                                $this->db->update($table, $data); 
                                $this->session->set_userdata('thongbao', 'Đã Sửa xong');
                            }else{
                                $this->db->insert($table, $data); 
                                $this->session->set_userdata('thongbao', 'Đã thêm');
                            }
                            redirect($this->base_url_trang.'/list');
                        }
                    }
                    
                    if($id){
                        $data = array('data'=>$this->home_model->get_one($table,array('id'=>$id)));
                    }else $data = array();
                    $this->load->view('mod_admin2/'.$page.'_edit',$data);//true la k hien chi tra ve
                    break;
               case 'xoa':
                    if($id){
                        $this->db->delete($table, array('id' => $id)); 
                        $this->session->set_userdata('thongbao', 'Đã thực hiện');
                    }
                    redirect($this->base_url_trang.'/list');
                    break;
            }
       }
    }
    function  khaibao($param){
        switch ($param) {
           case 'baiviet':
                $this->load->library(array('form_validation'));
                $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[10]|xss_clean');
                $this->form_validation->set_rules('alias', 'Alias', 'trim|xss_clean');
                $this->form_validation->set_rules('iddanhmuc', 'Danh Mục', 'trim|required|xss_clean');
                $this->form_validation->set_rules('publish', 'Đăng', 'trim|required|min_length[1]|xss_clean');
                $this->form_validation->set_rules('metades', 'Miêu tả', 'xss_clean');
                $this->form_validation->set_rules('metakey', 'Từ khóa', 'xss_clean');
                $this->form_validation->set_rules('introtext', 'Intro', 'trim|required|min_length[10]|max_length[300]|xss_clean');
                $this->form_validation->set_rules('fulltext', 'Nội Dung', 'trim|required|min_length[10]|xss_clean');
                break;
           default:
                break;
        }
    }
    function xuly_dulieu($data,$table,$param){
        switch ($table) {
           case 'baiviet':
                if(empty($data['alias']) || $data['alias'] == ''){
                    $data['alias'] = $this->chuyendoitv($data['title']);
                }
                if(!$param){// neu la them
                    $data['order'] = $this->home_model->get_number($table,array('iddanhmuc'=>$data['iddanhmuc']))+1;
                    $i = $this->home_model->get_number($table,array('alias'=>$data['alias']));
                }else{
                    $i = $this->home_model->get_number($table,array('alias'=>$data['alias']),array('id !='=>$param));
                }
                if ($i > 0){ $data['alias'] = $data['alias'].'-'.rand(1000,9999);}
                $data['image'] = $this->laydiachianh($data['fulltext']);
                break;
           default:
                break;
        }
        return $data;
    }
    function laydiachianh($image){        
            $src_start = strpos($image, 'src="') + 5;
            $src_end = strpos($image, '"', $src_start);
            $src = substr($image, $src_start, $src_end - $src_start);
            return $src;
    }
    function phantrang(){
        $this->load->library('pagination');
        $config['base_url'] = $this->base_url_trang.'/list';
        $config['total_rows'] = $this->total_rows;
        $config['per_page'] = $this->per_page;
        $config['uri_segment'] = 6;
        $config['num_links'] = 12;       
        //-----
        $this->pagination->initialize($config);
    
    }
    function chuyendoitv($str){
        $marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ","ặ","ẳ","ẵ",
                "è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ",
                "ì","í","ị","ỉ","ĩ",
                "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ","ờ","ớ","ợ","ở","ỡ",
                "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
                "ỳ","ý","ỵ","ỷ","ỹ",
                "đ",
                "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
                ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
                "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
                "Ì","Í","Ị","Ỉ","Ĩ",
                "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
                ,"Ờ","Ớ","Ợ","Ở","Ỡ",
                "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
                "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
                "Đ"," ","?",",","!"); 
                $marKoDau=array("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a",
                "e","e","e","e","e","e","e","e","e","e","e",
                "i","i","i","i","i",
                "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",
                "u","u","u","u","u","u","u","u","u","u","u",
                "y","y","y","y","y",
                "d",
                "A","A","A","A","A","A","A","A","A","A","A","A"
                ,"A","A","A","A","A",
                "E","E","E","E","E","E","E","E","E","E","E",
                "I","I","I","I","I",
                "O","O","O","O","O","O","O","O","O","O","O","O"
                ,"O","O","O","O","O",
                "U","U","U","U","U","U","U","U","U","U","U",
                "Y","Y","Y","Y","Y",
                "D","-","","","");
                $str=str_replace($marTViet,$marKoDau,$str);
        return strtolower($str);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */